package com.jit.BluetoothKey.activity.base;

import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.jit.BluetoothKey.R;
import com.jit.BluetoothKey.utils.UserFiled;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Administrator on 2018/1/25.
 * 基类封装
 */

public class BaseActivity extends AppCompatActivity {
    private Toolbar mToolbarTb;

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        EventBus.getDefault().register(this);  //注册EventBus
        mToolbarTb = (Toolbar) findViewById(R.id.tb_toolbar);
        if (mToolbarTb!=null) {
            setSupportActionBar(mToolbarTb);
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        initIntent();
        initView();
        initCondition();
        initData();
        initAdapt();
        initListner();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void exit(String event){
        if (event.equals(UserFiled.EXIT)){
            finish();
        }
    }

    /**
     *  返回按钮点击事件
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    /**
     * 初始化请求参数
     */
    protected void initCondition() {
    }

    /**
     * 初始化监听事件
     */
    protected void initListner() {
    }

    /**
     * 初始化 适配器
     */
    protected void initAdapt() {
    }

    /**
     * 初始化data
     */
    protected void initData() {
    }

    /**
     * 初始化View
     */
    protected void initView() {
    }

    /**
     * 跳转 intent 接收
     */
    protected void initIntent() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);  //注销Eventbus
    }
}
